<?php
require_once 'BaseMySQLDAO.php';

class GameStatisticDAO extends BaseMySQLDAO {

    public function __construct($context) {
        parent::__construct($context, 'game_statistic');
    }

    public function insertGameDownload($gameId) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "SELECT id FROM game_statistic WHERE game_id = :gameId ";
            $param = array(':gameId' => $gameId);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadOneRow($inSQL, $param);

            if ($result === false)
                $inSQL = "INSERT INTO game_statistic VALUES (null, :gameId, 1, null, null)";
            else
                $inSQL = "UPDATE game_statistic SET download = download + 1 WHERE game_id = :gameId ";

            $param = array(':gameId' => $gameId);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
}
?>